package qeug.shsh.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

//本次申请金额大写字段逻辑赋值
public class MoneyFormatterPlugin extends AbstractFormPlugin {
    private static final String[] CHINESE_NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] CHINESE_UNITS = {"", "拾", "佰", "仟"};
    private static final String[] CHINESE_BIG_UNITS = {"", "万", "亿", "万亿"};

    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String fieldKey = e.getProperty().getName();

        if ("qeug_amount".equals(fieldKey)) {
            //实付金额
            BigDecimal applyAmount = (BigDecimal) this.getModel().getValue("qeug_amount");
            String chineseAmount = convertToChinese(applyAmount);
            this.getModel().setValue("qeug_camount", chineseAmount);
            System.out.println();
        }else if ("supplier".equals(fieldKey)){
            for (ChangeData changeData : e.getChangeSet()) {
                if (changeData.getNewValue()!=null){
                    DynamicObject newsup = (DynamicObject)changeData.getNewValue();
                    String supNum = newsup.getString("number");
                    boolean containsSup = supNum.contains("Sup");
                    if (containsSup) {
                        this.getModel().setValue("supplier", "" ,changeData.getRowIndex());
                        this.getModel().setValue("qeug_union", "" ,changeData.getRowIndex());
                        this.getView().showErrorNotification("选择的供应商未同步MDM,不可选择！");
                    }
                }
            }

        }
    }

    public static String convertToChinese(BigDecimal amount) {
        if (amount.compareTo(BigDecimal.ZERO) == 0) {
            return "零元整";
        }

        String integerPart = amount.toBigInteger().toString();
        String decimalPart = String.format("%02d", amount.remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).intValue());

        StringBuilder result = new StringBuilder();

        // 处理整数部分
        result.append(convertIntegerPart(integerPart));

        // 处理小数部分
        if (!decimalPart.equals("00")) {
            result.append("元");
            result.append(convertDecimalPart(decimalPart));
        } else {
            result.append("元整");
        }

        return result.toString();
    }

    private static String convertIntegerPart(String integerPart) {
        StringBuilder result = new StringBuilder();
        int zeroCount = 0;
        int length = integerPart.length();

        for (int i = 0; i < length; i++) {
            int digit = integerPart.charAt(i) - '0';
            int unitIndex = (length - i - 1) % 4;
            int bigUnitIndex = (length - i - 1) / 4;

            if (digit == 0) {
                zeroCount++;
            } else {
                if (zeroCount > 0) {
                    result.append(CHINESE_NUMBERS[0]);
                    zeroCount = 0;
                }
                result.append(CHINESE_NUMBERS[digit]);
                result.append(CHINESE_UNITS[unitIndex]);
            }

            if (unitIndex == 0 && zeroCount < 4) {
                result.append(CHINESE_BIG_UNITS[bigUnitIndex]);
            }
        }

        return result.toString();
    }

    private static String convertDecimalPart(String decimalPart) {
        StringBuilder result = new StringBuilder();
        int firstDigit = decimalPart.charAt(0) - '0';
        int secondDigit = decimalPart.charAt(1) - '0';

        if (firstDigit != 0) {
            result.append(CHINESE_NUMBERS[firstDigit]);
            result.append("角");
        }

        if (secondDigit != 0) {
            result.append(CHINESE_NUMBERS[secondDigit]);
            result.append("分");
        }

        return result.toString();
    }
}
